package org.jeecg.common.api.vo;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.common.constant.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 *   接口返回数据格式
 * @author scott
 * @email jeecgos@163.com
 * @date  2019年1月19日
 */
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class MsgResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;


	private T data;

	private int code;

	private String message;

	private static Logger log = LoggerFactory.getLogger(Result.class);

	public MsgResult(T data) {
		this.code = CODE_SUCCESS;
		this.message = "成功";
		this.data = data;
	}

	public MsgResult() {
		this.code = CODE_SUCCESS;
		this.message = "成功";
	}

	public MsgResult(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public static <T> Result<T> ok() {
		return new Result<>();
	}

	public void failSystem() {
		this.code = CODE_FAIL_SYSTEM;
		this.message = "操作异常, 请稍后再试";
	}

	public void failSystem(String message) {
		failSystem();
		this.message = message;
	}

	public void failSystem(Exception e) {
		failSystem(e.getMessage());
//        if (e instanceof ClientAbortException) {
		if ("ClientAbortException".equals(e.getClass().getSimpleName())) {
			return;
		}
		log.error("系统错误: ", e);
	}

	public MsgResult failBiz(String message) {
		this.code = CODE_FAIL_BIZ;
		this.message = message;
		return this;
	}

	public static MsgResult success(String message,Object data) {
		MsgResult result = new MsgResult();
		result.code = CODE_SUCCESS;
		result.message = message;
		result.data=data;
		return  result;
	}
	public static MsgResult success(String message) {
		MsgResult result = new MsgResult();
		result.code = CODE_SUCCESS;
		result.message = message;
		return  result;
	}

	public static MsgResult error(String message) {
		MsgResult result = new MsgResult();
		result.code = CODE_FAIL_SYSTEM;
		result.message = message;
		return  result;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	public static final int CODE_AUTH_FAIL = 0, CODE_SUCCESS = 1, CODE_FAIL_SYSTEM = 2, CODE_FAIL_BIZ = 3;

	public boolean success() {
		return this.code == CODE_SUCCESS;
	}

	public MsgResult<T> success1() {
		this.code = CODE_SUCCESS;
		return this;
	}

	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}

}